package czk.concrent.juc.futuretask;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/3/29 21:09
 */
public class FutureTaskDemo1 {

    public static void main(String[] args) throws InterruptedException {

        FutureTask<String> futureTask=new FutureTask<>(()->{
            try {
                TimeUnit.SECONDS.sleep(5);
            }catch (Exception ex){
                System.out.println(ex.getClass().getName());
                System.out.println(ex.getMessage());
            }
            return "a";
        });

        FutureTaskDemo.executor.submit(futureTask);

        TimeUnit.SECONDS.sleep(3);

        System.out.println("超过3秒没返回结果，中断任务");

        futureTask.cancel(true);

    }
}
